package com.ciwong.mobilelib.widget;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ciwong.mobilelib.R;

/**
 * Created by Administrator on 2024/11/21.
 */

public class BuyServiceDialog extends CWDialog {

    private final int mDefaultMargin = 10;

    private final int mDefaultHeight = 45;

    private final int two = 2;

    private final float mDefaultTextSize = 17f;

    private LinearLayout mChooseMenu;
    private ImageView imageView;
    private ImageView imageViewClos;

    public BuyServiceDialog(Context context) {
        super(context);
        init();
    }

    private void init() {
        mChooseMenu = new LinearLayout(mContext);
        mChooseMenu.setOrientation(LinearLayout.VERTICAL);

        setContentView(mChooseMenu, true);
        setCanceledOnTouchOutside(true);
        setMenuText();
    }

    private void setMenuText() {
        mChooseMenu.removeAllViews();
        imageView = createImageView(R.mipmap.buy_service_dailog);
        mChooseMenu.addView(imageView);
        imageViewClos = createImageView(R.mipmap.close_buy_service);
        mChooseMenu.addView(imageViewClos);
    }

    public void setMenuOnClickListeners(View.OnClickListener on) {
        if (on != null) {
            imageView.setOnClickListener(on);
        }
    }

    public void setClosOnClickListeners(View.OnClickListener on) {
        if (on != null) {
            imageViewClos.setOnClickListener(on);
        }
    }

    private ImageView createImageView(int drawable) {
        ImageView imageView = new ImageView(mContext);
        setImageViewStyle(imageView);
        imageView.setBackgroundResource(drawable);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();

        layoutParams.topMargin = formatDimension(mDefaultMargin);
        layoutParams.bottomMargin = formatDimension(mDefaultMargin);

        imageView.setLayoutParams(layoutParams);
        return imageView;
    }

    private void setImageViewStyle(ImageView imageView) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = formatDimension(mDefaultMargin);
        layoutParams.rightMargin = formatDimension(mDefaultMargin);
        layoutParams.gravity = Gravity.CENTER;
        imageView.setLayoutParams(layoutParams);
    }

    /**
     * 显示菜单
     */
    public void show() {
        super.showFromCenter();
    }

    /**
     * dip 转换成px
     *
     * @param dip dp值
     * @return px值
     */
    private int formatDimension(int dip) {
        DisplayMetrics displayMetrics = mContext.getResources()
                .getDisplayMetrics();

        TypedValue typedValue = new TypedValue();
        int size = (int) typedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dip, displayMetrics);
        return size;
    }

    /**
     * sp 转换成px
     *
     * @param sp sp值
     * @return px值
     */
    private int formatTextSize(int sp) {
        DisplayMetrics displayMetrics = mContext.getResources()
                .getDisplayMetrics();

        TypedValue typedValue = new TypedValue();
        int size = (int) typedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                sp, displayMetrics);
        return size;
    }
}
